{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程3.2】  序列通用操作\n",
    "\n",
    "序列分类：可变序列list，不可变序列tuple、str\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 判断值是否属于序列\n",
    "\n",
    "lst = [1,2,3,4,5,6]\n",
    "a,b = 1,10\n",
    "print(a in lst)  # a 在 lst 序列中 , 如果 x 在 y 序列中返回 True。\n",
    "print(b not in lst)  # b 不在 lst 序列中 , 如果 x 不在 y 序列中返回 True。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 'a', 'b', 'c']\n",
      "[1, 2, 3, 1, 2, 3, 1, 2, 3] ['a', 'b', 'c', 'a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "# 序列链接与重复\n",
    "\n",
    "lst1 = [1,2,3]\n",
    "lst2 = ['a','b','c']\n",
    "print(lst1+lst2)  # \"+\"：序列的链接\n",
    "print(lst1*3,lst2*2)  # \"*\"：序列重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 3 0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 下标索引\n",
    "\n",
    "lst = [1,2,3,4,5,6,7,8,9,0]\n",
    "print(lst[0],lst[2],lst[9])  # 索引从0开始，eg lst[2]中，下标索引是2，指向lst的第3个值\n",
    "print(lst[-1])  # 索引-1代表最后一个值\n",
    "#print(lst[10])  # 这里一共有10个值，所以最大索引是9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "[1, 2, 3, 4, 5]\n",
      "[5, 6, 7, 8, 9, 0]\n",
      "[6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "\n",
    "lst = [1,2,3,4,5,6,7,8,9,0]\n",
    "print(lst[2:5])  # 切片可以理解成列表的值区间，且是一个左闭右开区间，这里lst[2:5]代表的区间是：索引2的值 - 索引4的值\n",
    "print(lst[:5])  # 左边无索引，代表从索引0开始\n",
    "print(lst[4:])  # 右边无索引，代表以最后一个值结束\n",
    "print(lst[5:-1])  # 索引5的值 - 倒数第二个值 （切片是右闭区间，注意了）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5]\n",
      "[1, 3, 5, 7, 9]\n",
      "[2, 4, 6, 8, 0]\n"
     ]
    }
   ],
   "source": [
    "# 步长\n",
    "\n",
    "lst = [1,2,3,4,5,6,7,8,9,0]\n",
    "print(lst[0:5:2])  # List[i:j:n]代表：索引i - 索引j，以n为步长\n",
    "print(lst[::2])  # 按照2为步长，从第一个值开始截取lst数据\n",
    "print(lst[1::2])  # 按照2为步长，从第二个值开始截取lst数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9 0 45\n",
      "2\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 序列的基本内置全局函数\n",
    "\n",
    "lst = [1,2,3,4,5,6,7,8,9,0]\n",
    "print(len(lst))  # 列表元素个数\n",
    "print(max(lst),min(lst),sum(lst))  # 返回列表的最大值、最小值、求和，这三个函数都只针对数字的list\n",
    "print(lst.index(3))  # .index(obj)方法：从列表中找出某个值第一个匹配项的索引位置\n",
    "\n",
    "lst = [1,1,2,3,3,4,4,4,4,5,6]\n",
    "print(lst.count(4))  # .count(obj)方法：计算值的出现次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① x=[1,2,3,1,[1],[1,1]]，请问x.count(1)为多少？\n",
    "\n",
    "② x=[1,2,5,1,“1”，“1”]，请问x.count(1)为多少？\n",
    "\n",
    "③ y=[45,67,89,45,34,22,1],请问y.index(45)等于多少？\n",
    "\n",
    "④ 创建一个列表，用type函数判断list[1]和list[2:5]的区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程3.3】  列表list常用操作\n",
    "\n",
    "序列是Python中最基本的数据结构，它支持字符，数字，字符串甚至可以包含列表（即嵌套）\n",
    "列表用 [ ] 标识，是 python 最通用的复合数据类型。\n",
    "列表中值的切片也可以用到变量 [头下标:尾下标] ，就可以截取相应的列表，从左到右索引默认 0 开始，从右到左索引默认 -1 开始，下标可以为空表示取到头或尾。\n",
    "加号 + 是列表连接运算符，星号 * 是重复操作\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "143 43 43\n",
      "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
      "[10, 101, 102]\n"
     ]
    }
   ],
   "source": [
    "# 列表的特征\n",
    "\n",
    "lst1 = [1,2,3,'a','b','c',[1,2,3]]\n",
    "# 可包含任意类型的对象：数值、字符串、列表等\n",
    "\n",
    "lst2 = [143,56,894,67,43]\n",
    "print(lst2[0],lst2[4],lst2[-1])\n",
    "# 通过下标索引访问序列中的值 → 序列的顺序是不能改变的,通过索引来定位列表的元素\n",
    "\n",
    "lst3 = [1,2,3]\n",
    "lst3 = lst3 * 3\n",
    "print(lst3)\n",
    "# 可随意变换列表长度，相当于随意指定新的列表\n",
    "\n",
    "lst4 = [1,2,['a','b']]\n",
    "# 可嵌套\n",
    "\n",
    "lst5 = [100,101,102]\n",
    "lst5[0] = 10\n",
    "print(lst5)\n",
    "# 可原位改变"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① [1,2,3]和[“1”, “2”, “3”]什么区别\n",
    "\n",
    "② 创建一个列表，包括3个人的数学成绩100，89,78\n",
    "\n",
    "③ 创建一个列表，包括5个中国城市名称\n",
    "\n",
    "④ 现有一个列表name = ['小明', '小红'] ,增加“团支书”的名字上去\n",
    "\n",
    "⑤ 现有一个列表m=[3.1,5.5,4,3,77,8]，用print分别输出第2、4个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 5) <class 'range'>\n",
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 列表与生成器\n",
    "\n",
    "print(range(5),type(range(5)))  \n",
    "# range()是生成器，指向了一个范围\n",
    "# range(5)代表指向了0,1,2,3,4这几个值\n",
    "# range(2,5)代表指向了2,3,4这几个值，注意这里不是使用:\n",
    "# range(0,10,2)代表指向了0,2,4,6,8这几个值，最后的2代表步长\n",
    "\n",
    "lst = list(range(5))\n",
    "print(lst)\n",
    "# 通过list()函数生成列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① range()生成的是不是都是整数?\n",
    "\n",
    "② 创建一个range，用type()函数查看其类型\n",
    "\n",
    "③ 哪种方式书写正确：x=range(5), x=range(4:7), x=range(4,8)\n",
    "\n",
    "④ 创建一个range，用print输出其值，看一下输出结果\n",
    "\n",
    "⑤ 用list()函数，将一个range范围变为列表\n",
    "\n",
    "⑥ m=range(10),请问m[2],m[-1],m[5]是多少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello']\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello', ['a', 'b', 'c']]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello', ['a', 'b', 'c'], 'a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "# 可变列表list常用操作 - 添加元素\n",
    "\n",
    "lst = list(range(10))\n",
    "lst.append('hello')\n",
    "print(lst)\n",
    "# list.append() 添加元素\n",
    "\n",
    "lst.append(['a','b','c'])\n",
    "print(lst)\n",
    "lst.extend(['a','b','c'])\n",
    "print(lst)\n",
    "# 添加多个元素用.extend()方法：用新列表扩展原来的列表，注意和append()方法的区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Jack', 'Tom', 'Tracy', 'Alex', 'White']\n",
      "['Jack', 'Tom', 'Tracy']\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 可变列表list常用操作 - 删除元素\n",
    "\n",
    "lst = ['Jack','Jack','Tom','Tracy','Alex','White']\n",
    "lst.remove('Jack')\n",
    "print(lst)\n",
    "# .remove方法：移除列表中某个值的第一个匹配项\n",
    "\n",
    "del lst[3:5]\n",
    "print(lst)\n",
    "# del语句：删除list的相应索引值\n",
    "\n",
    "lst.clear()\n",
    "print(lst)\n",
    "# 移除所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 1, 2, 'a', 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 1, 2, 'a', 3, [1, 2, 3], 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 可变列表list常用操作 - 插入元素\n",
    "\n",
    "lst = list(range(10))\n",
    "print(lst)\n",
    "lst.insert(3,'a')\n",
    "print(lst)\n",
    "# x.insert(i,m)方法：在索引i处插入m，这里索引3代表第四个值\n",
    "\n",
    "lst.insert(5,[1,2,3])\n",
    "print(lst)\n",
    "# 插入一个列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 'hello', 3, 4, 5, 6, 7, 8, 9] [0, 1, 'hello', 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 'hello', 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 可变列表list常用操作 - 复制\n",
    "\n",
    "lst = list(range(10))\n",
    "lst1 = lst\n",
    "lst1[2] = 'hello'\n",
    "print(lst,lst1)\n",
    "# lst，lst1指向同一个列表\n",
    "\n",
    "lst = list(range(10))\n",
    "lst2 = lst.copy()\n",
    "lst2[2] = 'hello'\n",
    "print(lst,lst2)\n",
    "# list.copy()方法：复制一个新的列表，lst，lst2指向两个列表（虽然两个列表值相同）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 12, 32, 45, 45, 66, 98] ['asd', 'cxv', 'ghv']\n",
      "[98, 66, 45, 45, 32, 12, 3] ['ghv', 'cxv', 'asd']\n",
      "[98, 66, 45, 45, 32, 12, 3] [3, 12, 32, 45, 45, 66, 98, 'hello']\n"
     ]
    }
   ],
   "source": [
    "# 可变列表list常用操作 - 排序\n",
    "\n",
    "lst1 = [12,45,32,98,45,66,3]\n",
    "lst2 = [\"asd\", \"cxv\", \"ghv\"]\n",
    "lst1.sort()\n",
    "lst2.sort()\n",
    "print(lst1,lst2)\n",
    "# 默认升序排序/字母顺序\n",
    "\n",
    "lst1.sort(reverse=True)\n",
    "lst2.sort(reverse=True)\n",
    "print(lst1,lst2)\n",
    "# 参数reverse：反向排序，针对数字\n",
    "\n",
    "lst3 = sorted(lst1)\n",
    "lst3.append('hello')\n",
    "print(lst1,lst3)\n",
    "# 函数sorted():排序并复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1997 ('a', 'b')\n",
      "4\n",
      "('physics', 'chemistry', 1997, 2000, 1, 2, 3, 4, 5)\n",
      "(50, 50, 50)\n",
      "5 1\n",
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
     ]
    }
   ],
   "source": [
    "# 讲一讲元祖tuple ：不可变的“列表”\n",
    "\n",
    "tup1 = ('physics', 'chemistry', 1997, 2000);\n",
    "tup2 = (1, 2, 3, 4, 5 );\n",
    "tup3 = \"a\", \"b\", \"c\", \"d\"  # 可以不加括号\n",
    "tup4 = (50,)  # 元组中只包含一个元素时，需要在元素后面添加逗号\n",
    "\n",
    "print(tup1[2],tup3[:2])  # 索引、切片和list一样\n",
    "\n",
    "del tup3 # 元祖不能单独删除内部元素（不可变性），但可以del语句来删除整个元祖\n",
    "#print(tup3)\n",
    "\n",
    "print(len(tup1))\n",
    "print(tup1 + tup2)\n",
    "print(tup4 * 3)\n",
    "print(max(tup2),min(tup2))\n",
    "# 序列通用函数\n",
    "\n",
    "lst = list(range(10))\n",
    "tup5 = tuple(lst)\n",
    "print(tup5)\n",
    "# tuple()函数：将列表转换为元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① 分别用append、extend方法，给一个序列增加一个元素1\n",
    "\n",
    "② x.append([1,2,3])和x.extend([1,2,3])的区别\n",
    "\n",
    "③ x.append('abc')和x.extend('abc')的区别\n",
    "\n",
    "④ 创建某一列表，在列表中间插入一个值\n",
    "\n",
    "⑤ 创建一个随机数值列表，按照降序排列\n",
    "\n",
    "⑥ sort()和sorted()什么区别\n",
    "\n",
    "⑦ s=[32,45,63,12,45,89,77], s.remove(45),  del s[2], s.pop(4)分别什么区别\n",
    "\n",
    "⑧ m=[4,5,6,73,4,4,4,4,2]，删除2，删除所有的4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程3.4】  文本序列str常用操作\n",
    "\n",
    "字符串是 Python 中最常用的数据类型。我们可以使用引号('或\")来创建字符串\n",
    "字符串也是序列：文本序列\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串引号\n",
    "\n",
    "str1 = \"abc\"\n",
    "str2 = 'abc'\n",
    "str3 = 'my name is \"fatbird\"'\n",
    "# 双引号单引号无区别，但文本中有引号的时候要相互交替使用\n",
    "\n",
    "str4 = '''hello! how are you?\n",
    "          I`m fine, thank you.'''\n",
    "# 需要多行字符串时候用三引号 ''' ''',\"\"\" \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "' \"\n",
      "hello\n",
      "how do you do?\n",
      "\\\n"
     ]
    }
   ],
   "source": [
    "# 转义字符：\\\n",
    "\n",
    "print('\\'', '\\\"')  # \\',\\\" ：分别输出单引号，双引号\n",
    "print('hello\\nhow do you do?')  # \\n：空行\n",
    "print('\\\\')  # 输出\\，所以文件路径用“/”或者“\\\\”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "我很帅没错\n",
      "handsome handsome handsome handsome \n",
      "c g\n",
      "ab\n",
      "aceg\n",
      "6\n",
      "st长度为： 7\n"
     ]
    }
   ],
   "source": [
    "# 序列通用功能\n",
    "\n",
    "print('a' in 'abc')  # in/not in ：判断是否存在\n",
    "print('我很帅' + \"没错\")  # 文本连接\n",
    "print('handsome '*4)  # 文本复制\n",
    "\n",
    "st = 'abcdefg'\n",
    "print(st[2],st[-1])\n",
    "print(st[:2])\n",
    "print(st[::2])\n",
    "# 索引、切片、步长\n",
    "\n",
    "print(st.index('g'))  # .index()方法\n",
    "print('st长度为：',len(st))  # 计算字符串长度（思考这里能否把逗号换为\"+\"）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i`m handsome!\n",
      "i`m ugly!\n",
      "heheha\n",
      "['poi01', '116.446238', '39.940166']\n",
      "poi01-116.446238-39.940166\n",
      "True False\n",
      "ABDEAJC KLM\n",
      "abdeajc klm\n",
      "AbdeaJC Klm\n",
      "Abdeajc klm\n",
      "True\n",
      "True\n",
      "avd\n"
     ]
    }
   ],
   "source": [
    "# 字符串常用功能\n",
    "\n",
    "st = \"i`m handsome!\"\n",
    "st2 = st.replace('handsome','ugly')\n",
    "print(st)\n",
    "print(st2)\n",
    "st = 'hahaha'\n",
    "st2 = st.replace('ha','he',2)\n",
    "print(st2)\n",
    "# str.replace(old,new,count)：修改字符串，count：更换几个\n",
    "\n",
    "st = \"poi01,116.446238,39.940166\"\n",
    "lst = st.split(',')\n",
    "print(lst)\n",
    "# str.split(obj)：拆分字符串，生成列表\n",
    "\n",
    "lst = ['poi01', '116.446238', '39.940166']\n",
    "m = '-'\n",
    "st = m.join(lst)\n",
    "print(st)\n",
    "# str.join()：连接字符串，对象为列表\n",
    "\n",
    "st = 'abcdefg'\n",
    "print(st.startswith('a'), st.endswith('f'))\n",
    "# str.startswith(“str”)  判断是否以“str”开头；str.endswith (“str”)  判断是否以“str”结尾\n",
    "\n",
    "st = 'aBDEAjc kLM'\n",
    "print(st.upper())  # 全部大写\n",
    "print(st.lower())  # 全部小写\n",
    "print(st.swapcase())  # 大小写互换\n",
    "print(st.capitalize())  # 首字母大写\n",
    "\n",
    "st = '1234567'\n",
    "print(st.isnumeric())  # 如果 string 只包含数字则返回 True,否则返回 False.\n",
    "\n",
    "st = 'DNVAK'\n",
    "print(st.isalpha())  # 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False\n",
    "\n",
    "st = 'avd   '\n",
    "print(st.rstrip())  # 删除字符末尾的空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FATBIRD is 好人\n",
      "this is 4\n",
      "this is hehe\n",
      "this is 4.200000\n"
     ]
    }
   ],
   "source": [
    "# 格式化字符：在字符串中插入变量\n",
    "\n",
    "name = \"FATBIRD\"\n",
    "truth = \"%s is 好人\" %name\n",
    "print(truth)\n",
    "# %s 表示我们这里要插入一个变量（字符串），%name代表这里我们插入name → 所以，两个%解决问题\n",
    "\n",
    "x = 4   \n",
    "y = \"hehe\"  \n",
    "z = 4.2\n",
    "print(\"this is %i\" %x)\n",
    "print(\"this is %s\" %y)\n",
    "print(\"this is %f\" %z)\n",
    "# %s的s有什么含义：%s %z %f都是格式化字符串，s/i/f分别代表插入字符串的源数据类型：整型/字符型/浮点型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi is 3.141593\n",
      "pi is 3.14\n",
      "pi is 10\n",
      "pi is 11\n",
      "have fun +100\n",
      "have fun -0.01\n",
      "have fun  100\n",
      "have fun +100\n",
      "have fun -0.01\n",
      "have fun 1.23e+02\n",
      "have fun 1.2312E+02\n",
      "have fun 123.123\n",
      "have fun 1.2\n"
     ]
    }
   ],
   "source": [
    "# 格式化字符：数字格式化的那些坑\n",
    "\n",
    "m = 3.1415926 \n",
    "print(\"pi is %f\"  %m)\n",
    "print(\"pi is %.2f\" %m)\n",
    "# 我只想输出2位小数:%.2f，此处是四舍五入！\n",
    "\n",
    "m = 10.6\n",
    "print(\"pi is %i\"  %m)\n",
    "print(\"pi is %.0f\" %m)\n",
    "# 区别：%i 不四舍五入，直接切掉小数部分\n",
    "\n",
    "m = 100 \n",
    "print(\"have fun %+i\"  %m)\n",
    "print(\"have fun %.2f\"  % -0.01)\n",
    "# 显示正号，负号根据数字直接显示\n",
    "\n",
    "m = 100 \n",
    "print(\"have fun % i\"  %m)\n",
    "print(\"have fun % +i\"  %m)\n",
    "print(\"have fun % .2f\"  %-0.01)\n",
    "# 加空格，空格和正好只能显示一个\n",
    "\n",
    "m = 123.123123123 \n",
    "print(\"have fun %.2e\"  %m)\n",
    "print(\"have fun %.4E\"  %m)\n",
    "# 科学计数法 %e  %E\n",
    "\n",
    "m1 = 123.123123123\n",
    "m2 = 1.2\n",
    "print(\"have fun %g\"  %m1)\n",
    "print(\"have fun %g\"  %m2)\n",
    "# 小数位数少的时候自动识别用浮点数，数据复杂的时候自动识别用科学计数法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User ID: root\n",
      "a 呵呵 b\n",
      "abc \n",
      " abca \n",
      "\n",
      "我的工作是设计\n",
      "abcdef abc{}\n",
      "4.123000 \n",
      " 4.12 \n",
      " 4.123000e+00 \n",
      " 100 \n",
      " 412.300000% \n",
      " 10\n"
     ]
    }
   ],
   "source": [
    "# 更强大的格式化方法.format\n",
    "\n",
    "print(\"User ID: {0}\".format(\"root\"))\n",
    "print(\"{} 呵呵 {}\".format(\"a\",\"b\"))\n",
    "# {} 这里代表占位符，其中可以有数字也可以没有\n",
    "\n",
    "print(\"{}{}{}\".format('a','b','c'),'\\n',\n",
    "     \"{0}{1}{2}{0}\".format('a','b','c'),'\\n')\n",
    "#print(\"{}{}{}{}\".format('a','b','c'))\n",
    "# {}和{0}的区别:都是占位符，后者有了明确指定\n",
    "\n",
    "print(\"我的工作是{work}\".format(work = '设计'))\n",
    "# 也可以用变量来指示\n",
    "\n",
    "x=\"abc{}\"\n",
    "a = x.format(\"def\")\n",
    "print(a,x)\n",
    "# .format()生成新的值吗？？\n",
    "\n",
    "print(\"{:f}\".format(4.123),'\\n',\n",
    " \"{:.2f}\".format(4.123),'\\n',\n",
    " \"{:e}\".format(4.123),'\\n',\n",
    " \"{:.0f}\".format(99.9),'\\n',\n",
    " \"{:%}\".format(4.123),'\\n',\n",
    " \"{:d}\".format(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "小作业\n",
    "\n",
    "① 三引号创建一个有提行的文本，print()函数输出\n",
    "\n",
    "② 申明一个字符串变量，值为一个文件路径，注意操作习惯\n",
    "\n",
    "③ 以下输出分别是多少：33+\"22\"; 33+int(\"22\"); \"22\" + str(55); \n",
    "\n",
    "④ m=“a,b,c”,m.split()会输出什么结果？（不加\",\"）\n",
    "\n",
    "⑤ 再回想一下：.split和.join的输出分别是什么类型的数据\n",
    "\n",
    "⑥ 如果我想打%怎么办？\n",
    "\n",
    "⑦ 这样书写正确吗，为什么？ print(\"abc%s\") % \"nn\"\n",
    "\n",
    "⑧ 这样书写正确吗，为什么？ print(245%f % 123)\n",
    "\n",
    "⑨ \"我的工作是…我喜欢…\" 把…替换成正确的内容    "
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
